#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>

int g_val = 10;

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        // child process
        int count = 0;
        while(1)
        {
            printf("I am a child process! PID : %d,PPID : %d,g_val = %d,&g_val = %p\n",getpid(),getppid(),g_val,&g_val);
            sleep(1);
            count++;
            if(count == 2)
            {
                g_val = 7;
                printf("I am a child process,g_val has changed!\n");
            }
        }       
    }
    else 
    {
        // parent process
        while(1)
        {
            printf("I am a parent process! PID : %d,PPID : %d,g_val = %d,&g_val = %p\n",getpid(),getppid(),g_val,&g_val);
            sleep(1);
        }
    }
    return 0;
}


//// 验证堆和栈的增长方向
//int un_g_val;
//int g_val=10;
//
//int main(int argc,char *argv[],char *env[])
//{
//    printf("code addr           :%p\n",main);
//    printf("init global addr    :%p\n",&g_val);
//    printf("uninit global addr  :%p\n",&un_g_val);
//    char *m1 = (char*)malloc(10);
//    char *m2 = (char*)malloc(10);
//    char *m3 = (char*)malloc(10);
//    char *m4 = (char*)malloc(10);
//    printf("heap addr           :%p\n",m1);
//    printf("heap addr           :%p\n",m2);
//    printf("heap addr           :%p\n",m3);
//    printf("heap addr           :%p\n",m4);
//    printf("stack addr          :%p\n",&m1);
//    printf("stack addr          :%p\n",&m2);
//    printf("stack addr          :%p\n",&m3);
//    printf("stack addr          :%p\n",&m4);
//    for(int i=0;i<argc;++i)
//    {
//         printf("argv addr           :%p\n",argv[i]);
//    }
//    for(int i=0;env[i];++i)
//    {
//         printf("env addr            :%p\n",env[i]);
//    }
//    return 0;
//}



//// 验证程序地址空间的分布
//int un_g_val;
//int g_val=10;
//
//int main(int argc,char *argv[],char *env[])
//{
//    printf("code addr           :%p\n",main);
//    printf("init global addr    :%p\n",&g_val);
//    printf("uninit global addr  :%p\n",&un_g_val);
//    char *m1 = (char*)malloc(10);
//    printf("heap addr           :%p\n",m1);
//    printf("stack addr          :%p\n",&m1);
//    for(int i=0;i<argc;++i)
//    {
//         printf("argv addr           :%p\n",argv[i]);
//    }
//    for(int i=0;env[i];++i)
//    {
//         printf("env addr            :%p\n",env[i]);
//    }
//    return 0;
//}
